//add map area panelId 
var dopBtnRestaurantAdd = "";
var mapManager = null;
var isModify = false;
$("#dop_datagrid_restaurant").datagrid({
	url:"",
	height: 350,
	pagination:true,
	fitColumns:false,
	singleSelect:false,
	fit:true,
	pageSize: 10,
	pageList:[10,30,50,100],
	sortOrder:'desc',
	rownumbers:true,
	showFooter:true,
	frozenColumns:[[]],
	columns:[[
	    {
	    	field:'ck',
	    	align:"center",
	    	checkbox:true
	    	
    	},
		{
			field: "id",
			width: 150,
			title: "主键", 
			hidden:true
		},
		{
			field: "resName", 
			width: 150, 
			title: "商户名称",
			halign:"center",
			align:'left'
		},
		{
			field: "resEnableText",
			width: 150, 
			title: "是否启用",
			align:'center'
		}
	]]
});

init();

//click pagination
var p = $("#dop_datagrid_restaurant").datagrid('getPager');
$(p).pagination({
    onSelectPage: function (pageNumber, pageSize) {
    	var params = $.extend({pageNum:pageNumber, pageSize:pageSize}, getQueryParams());
		EapTools.doAction("restaurantController.do?queryList", params, true, function(data) {
			$("#dop_datagrid_restaurant").datagrid('loadData', {"rows": data.list, "total": data.count}|| {});
		});
    }
});


//bind add event
$("#dop_btn_restaurant_add").click(function() {
	isModify = false;
	$("input[id='resName']").val("");
	$("#resDesc").val("");
    //show map dialog
	dopBtnRestaurantAdd = jQuery.layer({
		type : 1,
		title : '<div><span style="padding-left: 14px;line-height: 14px;">添加商户</span></div>',
		shadeClose : true,
		maxmin : false,
		fix : false,
		area : [ 500, 200 ],
		page : {
			dom : '#dop_restaurant_add_panel'
		}
	});
});

//bind modify event
var oldName="";
$("#dop_btn_restaurant_modify").click(function() {
	var selections = $("#dop_datagrid_restaurant").datagrid("getSelections");
	if (selections && selections.length > 0) {
		if (selections.length > 1) {
			layer.tips('请选择一行数据', this , {guide: 2, time: 2});
		} else {
             // 显示地图div
			$("#dop_btn_restaurant_add").click();
			//获得此项记录的坐标字符串						   						    
			var resName = selections[0].resName;
			oldName= selections[0].resName;
			//更新区域名称的显示
			$("input[id='resName']").val(resName);
			$("#resDesc").val(selections[0].resDesc);
			isModify = true;
		}
	} else
		layer.tips('请选择要修改行', this, {
			guide : 2,
			time : 2
		});
});

//bind stop event
$("#dop_btn_restaurant_stop").click(function() {
	var selections = $("#dop_datagrid_restaurant").datagrid("getSelections");
	if (selections && selections.length > 0)
		layer.confirm('如果停用，该商户下的店面都将停用，确认执行此操作吗？', function() {
			EapTools.doAction("restaurantController.do?stop", {"rowList" : JSON.stringify(selections)}, true, function(data) {
				if (data.length==0) {
					EapTip.msgOk("停用成功！");
					init();
				}else{
					var tips="";
					for(var i=0;i<data.length;i++){
						var json=data[i];
						tips+=json.res_name+"下的"+json.sto_name+"店面中有<font color='red'>"+json.count+"</font>笔未完成的订单！<br/>";
					}
					EapTip.alertError("操作失败，原因如下:<br/>"+tips);
				}
			});
		});
	else
		layer.tips('请选择要停用的行', this, {
			guide : 2,
			time : 2
		});
});
//bind open event
$("#dop_btn_restaurant_open").click(function() {
	var selections = $("#dop_datagrid_restaurant").datagrid("getSelections");
	if (selections && selections.length > 0)
		layer.confirm('如果启用，该商户下的店面都将启用，确认执行此操作吗？', function() {
			EapTools.doAction("restaurantController.do?open", {"rowList" : JSON.stringify(selections)}, true, function(data) {
				if (data.success) {
					EapTip.msgOk("启用成功！");
					init();
				}else{
					EapTip.msgOk("操作失败，请重试！");
				}
			});
		});
	else
		layer.tips('请选择要启用的行', this, {
			guide : 2,
			time : 2
		});
});

//验证商户名称
var resNameFlag=false;
function checkResName(){
	var resName = $.trim($("input[id='resName']").val());
	if(isModify){
		if(oldName==resName){
			resNameFlag=true;
			return;
		}
	}
	if(resName.length==0){
		layer.tips('商户名不得为空', document.getElementById("resName"), {
			guide : 2,
			time : 2
		});
		resNameFlag=false;
		return;
	}
	if(resName.length>80){
		layer.tips('商户名不得超过80个字符', document.getElementById("resName"), {
			guide : 2,
			time : 2
		});
		resNameFlag=false;
		return;
	}
	EapTools.doAction("restaurantController.do?checkResName", 
			{"resName" : resName}, true, 
				function(data) {
					resNameFlag=data.success;
					if(!data.success){
						layer.tips('该商户已经存在，请您换个名称', document.getElementById("resName"), {
							guide : 2,
							time : 2
						});
					}
			});
}
/**
 * 判断商户简介
 */
var resDescFlag=true;
function checkResDesc(){
	var resDesc=$("#resDesc").val();
	if(resDesc.length>200){
		layer.tips('商户备注不得超过200个字符', document.getElementById("resDesc"), {
			guide : 2,
			time : 2
		});
		resDescFlag=false;
		return ;
	}
	resDescFlag=true;
}
//添加面板 确定按钮
$("#dop_restaurant_add_panel_ok").click(function() {
	var resName = $.trim($("input[id='resName']").val());
	var resDesc=$("#resDesc").val();
	checkResName();
	checkResDesc();
	if (resNameFlag&&resDescFlag)
		if (isModify)
			EapTools.doAction("restaurantController.do?modify", {"resName" : resName,"resDesc":resDesc, "id" : $("#dop_datagrid_restaurant").datagrid("getSelections")[0].id}, true, function(data) {
				layer.close(dopBtnRestaurantAdd);
				EapTip.msgOk("修改成功");
				$("input[id='resName']").val("");
				init();
			});
		else 
			EapTools.doAction("restaurantController.do?create", {"resName" : resName}, true, function(data) {
				layer.close(dopBtnRestaurantAdd);
				EapTip.msgOk("添加成功");
				$("input[id='resName']").val("");
				init();
			});
});

//添加面板 取消按钮
$("#dop_restaurant_add_panel_cancel").click(function() {
	layer.close(dopBtnRestaurantAdd);
});

//bind query event
$("#dop_btn_restaurant_query").click(function() {
	init();
});


//get params
function getQueryParams() {
	return {
		resName : $("input[code='resName']").val()
	};
}

//init
function init() {
	//init
	var options = $("#dop_datagrid_restaurant").datagrid('getPager').data("pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({pageNum : 1, pageSize : pageSize}, getQueryParams());
	EapTools.doAction("restaurantController.do?queryList", params,true, function(data) {
		$("#dop_datagrid_restaurant").datagrid('loadData', {"rows" : data.list, "total" : data.count} || {});
		var p = $("#dop_datagrid_restaurant").datagrid('getPager');
		$("td:last", p).find("a").click();
	});
}